package cn.usian.basicInformation.car.service.impl;

import cn.usian.basicInformation.car.mapper.CarMapper;
import cn.usian.basicInformation.car.service.ICarService;
import cn.usian.basicInformation.sensitivewords.config.SensitiveWords;
import cn.usian.basicInformation.sensitivewords.mapper.SensitiveWordsMapper;
import cn.usian.config.ResultException;
import cn.usian.dto.basicInformation.car.BaseCarTypeManageDto;
import cn.usian.pojo.Result;
import cn.usian.pojo.basicInformation.car.BaseCarTypeManage;
import cn.usian.vo.basicInformation.car.BaseCarTypeManageVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CarServiceImpl implements ICarService {
    @Autowired
    private CarMapper carMapper;

    @Autowired
    private SensitiveWordsMapper sensitiveWordsMapper;

    @Autowired
    private SensitiveWords sensitiveWords;

    /**
    * 查询车辆信息
     */
    @Override
    public PageInfo<BaseCarTypeManageVo> list(BaseCarTypeManageDto carTypeManageDto) {
        PageHelper.startPage(carTypeManageDto.getPageNum(),carTypeManageDto.getPageSize());
        // 获取车辆类型
        String carTypeId = carTypeManageDto.getCarTypeId();
        log.info("carTypeId:{}",carTypeId);
        String[] carTypeSplit = carTypeId.split(",");
        List<String> carTypeIds = Arrays.asList(carTypeSplit);
        log.info("carTypeIds:{}",carTypeIds);
        // 获取车辆能源类型
        String carEnergyId = carTypeManageDto.getCarEnergyId();
        log.info("carEnergyId:{}",carEnergyId);
        String[] carEnergySplit = carEnergyId.split(",");
        List<String> carEnergyIds = Arrays.asList(carEnergySplit);
        log.info("carEnergyIds:{}",carEnergyIds);
        // 查询车辆信息
        List<BaseCarTypeManageVo> list = carMapper.list(carTypeIds,carEnergyIds,carTypeManageDto.getVehicleModel(),carTypeManageDto.getCarBrand(),carTypeManageDto.getCarManufactor());
        log.info("list:{}",list);
        PageInfo<BaseCarTypeManageVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
    * 根据id查询车辆信息
     */
    @Override
    public BaseCarTypeManage findById(long id) {
        return carMapper.findById(id);
    }

    /**
    * 保存车辆信息
     */
    @Override
    public Result save(BaseCarTypeManage carTypeManage) {
        Result result = validateCarType(carTypeManage);
        if(result.getCode() != 200){
            return Result.error(result.getMsg());
        }
        if (carTypeManage.getId() != null) {
            carTypeManage.setUpdateTime(new Date());
            Integer update = carMapper.update(carTypeManage);
            if (update > 0) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } else {
            carTypeManage.setUpdateTime(new Date());
            Integer add = carMapper.add(carTypeManage);
            if (add > 0) {
                return Result.ok();
            } else {
                return Result.error();
            }
        }
    }

    /**
    * 校验车辆类型信息
     */
    private Result validateCarType(BaseCarTypeManage carTypeManage) {
        if (carTypeManage.getCarTypeId() == null) {
            logAndThrow("车辆类型不能为空");
        }
        if (carTypeManage.getCarEnergyId() == null) {
            logAndThrow("车辆能源类型不能为空");
        }
        if (StringUtils.isEmpty(carTypeManage.getCarBrand())) {
            logAndThrow("车辆品牌不能为空");
        }
        if (StringUtils.isEmpty(carTypeManage.getCarManufactor())) {
            logAndThrow("车辆制造厂商不能为空");
        }
        if (StringUtils.isEmpty(carTypeManage.getVehicleModel())) {
            logAndThrow("车辆型号不能为空");
        }
        Result result = sensitiveWords.sensitiveWords(carTypeManage.getVehicleModel());
        log.info("sensitiveWordsResult:{}",result);
        if(result.getCode() != 200){
            return Result.error(result.getMsg());
        }
        return result;
    }

    /*
    * 日志打印并抛出异常
     */
    private void logAndThrow(String message) {
        log.error(message);
        throw new ResultException(message);
    }
}
